// pagesWorkOrderReviewReject/pagesWorkOrderReviewReject.js
const {
  WorkOrderReviewApi
} = require('../../common/apis/workOrder')
const mixin_formItemType = require('../../common/mixins/formItemType')
const {
  DIC_KEY
} = require('../../config/dicKey.config')
import {
  SUB_PAGES
} from '../../config/router.config'
import {
  getDicList
} from '../../utils/dic'
import {
  WxValidate
} from '../../utils/WxValidate'
Page({
  behaviors: [mixin_formItemType],

  /**
   * 页面的初始数据
   */
  data: {
    // 工单号
    orderNum: '',
    // 驳回原因
    rejectReason: '',
    // 驳回说明
    rejectDescribe: '',
    rejectReasonList: getDicList(DIC_KEY.obs_no_examine),
    rules: {
      rejectReason: {
        required: true
      }
    },
    message: {
      rejectReason: {
        required: '请选择驳回原因'
      }
    }
  },
  changeRadioGroup(e) {
    this.setData({
      rejectReason: e.detail.value
    })
  },
  onCancel() {
    wx.navigateBack()
  },
  onSubmit() {
    const {
      orderNum,
      rejectReason,
      rejectDescribe
    } = this.data
    const data = {
      orderNum,
      rejectReason,
      rejectDescribe
    }
    const validate = new WxValidate(this.data.rules, this.data.message)
    if (!validate.checkForm(data)) {
      const error = validate.errorList[0]
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
      return false
    }
    WorkOrderReviewApi.reject(data, ({
      code
    }) => {
      if (code === 200) {
        wx.redirectTo({
          url: SUB_PAGES.WorkOrder.WORK_ORDER_REVIEW_INDEX.router,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const {
      orderNum
    } = options
    this.setData({
      orderNum: orderNum
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})